home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / oath.lha / oath / src / characterSet.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-29  |  5.1 KB  |  212 lines

  1. //***************************************************************************
  2. //             OATH :: Object-oriented Abstract Type Hierarchy
  3. //***************************************************************************
  4. //
  5. //  Copyright (C) 1991, 1990  Texas Instruments Incorporated
  6. //  Permission is granted to any individual or institution
  7. //  to use, copy, modify, and distribute this software,
  8. //  provided that this complete copyright and permission notice
  9. //  is maintained, intact, in all copies and supporting documentation.
  10. //
  11. //  Texas Instruments Incorporated provides this software "as is"
  12. //  without express or implied warranty.
  13. //
  14. //***************************************************************************
  15. //  characterSet (characterSetA, characterSetG)
  16. //
  17. //  History:
  18. //    07/91  Brian M Kennedy  import, export, typeRegister
  19. //    06/91  Brian M Kennedy  New macros & format; remove printDiagnostic
  20. //    04/91  Brian M Kennedy  Derive from finiteSet (was a set)
  21. //    12/90  Brian M Kennedy  Original
  22. //
  23. //***************************************************************************
  24.  
  25. #include "copyright.h"
  26.  
  27. #include <oath/characterSet.h>
  28.  
  29. #include <iostream.h>
  30.  
  31. /////////////////////////////////////////////////////////////////////////////
  32. // characterSet Outlines
  33.  
  34. OUTLINES(characterSet, finiteSet)
  35.  
  36. unsigned char characterSetG::BitMasks [8] = {1, 2, 4, 8, 16, 32, 64, 128};
  37.  
  38. // characterSet Accessors //////////
  39.  
  40.     void characterSetG::
  41. setbits(unsigned char S, unsigned char B)
  42.    {for(unsigned char C = S; C <= B; ++C)
  43.         setbit(C);
  44.    }
  45.  
  46.     void characterSetG::
  47. flipbits()
  48.    {for(int I = 0; I < 32; ++I)
  49.         Bytes[I] ^= 255;
  50.    }
  51.  
  52. // Constructors //////////
  53.  
  54.     characterSetG::
  55. characterSetG (int IsConst) // IsConst = FALSE);
  56.    :finiteSetG(IsConst)
  57.    {for(int I = 0; I < 32; ++I)
  58.         Bytes[I] = 0;
  59.    }
  60.  
  61.     characterSetG::
  62. characterSetG (const unsigned char iBytes[32], int IsConst) // = FALSE
  63.    :finiteSetG(IsConst)
  64.    {for(int I = 0; I < 32; ++I)
  65.         Bytes[I] = iBytes[I];
  66.    }
  67.  
  68.     characterSetG::
  69. characterSetG (const stringG* S, int IsConst) //  IsConst = FALSE);
  70.    :finiteSetG(IsConst)
  71.    {for(int I = 0; I < 32; ++I)
  72.         Bytes[I] = 0;
  73.     if(!S->isEmpty())
  74.        {int Invert = FALSE;
  75.         stringPosA P = (stringPosA&)(S->makePos(0, 0));
  76.     if((*P).value() == '^')
  77.        {Invert = TRUE;
  78.         ++P;
  79.        }
  80.     if(P())
  81.        {unsigned char LastValue;
  82.         while(1)
  83.            {setbit(LastValue = (*P).value());
  84.             ++P;
  85.         if(!P())
  86.                 break;
  87.         if((*P).value() == '-')
  88.            {++P;
  89.             if(!P())
  90.                {setbit('-');
  91.                 break;
  92.                }
  93.             setbits(LastValue, (*P).value());
  94.             ++P;
  95.             if(!P())
  96.                 break;
  97.            }
  98.            }
  99.        }
  100.     if(Invert)
  101.         flipbits();
  102.        }
  103.    }
  104.  
  105.  
  106. // oathCore Operations //////////
  107.  
  108.     void characterSetG::
  109. export (exportP& X) const
  110.    {X.writeType(TypeName);
  111.     X.stream().put(char(isConst()));
  112.     for(int I = 0; I < 32; ++I)
  113.         X.stream().put(Bytes[I]);
  114.    }
  115.  
  116.     objA characterSetG::
  117. import (importP& M)
  118.    {char MakeConst = M.stream().get();
  119.     unsigned char Bytes[32];        // speedup by writing a constructor
  120.     for(int I = 0; I < 32; ++I)         // to do this directly
  121.         Bytes[I] = M.stream().get();
  122.     return new characterSetG (Bytes, MakeConst);
  123.    }
  124.  
  125.  
  126. // obj Operations //////////
  127.  
  128.     int characterSetG::
  129. isEqual (const objG*) const
  130.    {ensure(FALSE, "Sorry, not implemented!");
  131.     return FALSE;
  132.    }
  133.  
  134.  
  135. // bag Operations //////////
  136.  
  137.     int characterSetG::
  138. isEmpty () const 
  139.    {for(int I = 0; I < 32; ++I)
  140.     if(Bytes[I]) return TRUE;
  141.     return FALSE;
  142.    }
  143.  
  144.     int characterSetG::
  145. count () const 
  146.    {int Count = 0;
  147.     for(int I = 0; I < 32; ++I)
  148.        {switch(Bytes[I])
  149.        {case  1: case  2: case  4: case  8: case 16:
  150.         Count += 1; break;
  151.         case  3: case  5: case  6: case  9: case 10:
  152.         case 12: case 17: case 18: case 20: case 24:
  153.         Count += 2; break;
  154.         case  7: case 11: case 13: case 14: case 19:
  155.         case 21: case 22: case 25: case 26: case 28:
  156.         Count += 3; break;
  157.         case 15: case 23: case 27: case 29: case 30:
  158.         Count += 4; break;
  159.         case 31:
  160.         Count += 5; break;
  161.        }
  162.        }
  163.     return Count;
  164.    }
  165.  
  166.     void characterSetG::
  167. apply (void (*)(objA)) const
  168.    {ensure(FALSE, "Sorry, not implemented!");}
  169.  
  170.     bagG* characterSetG::
  171. applyX (objA (*)(objA), bagG* B) const
  172.    {ensure(FALSE, "Sorry, not implemented!");
  173.     return B;
  174.    }
  175.  
  176.     bagG* characterSetG::
  177. applyX (bagA (*)(objA), bagG* B) const
  178.    {ensure(FALSE, "Sorry, not implemented!");
  179.     return B;
  180.    }
  181.  
  182.  
  183. // set Operations //////////
  184.  
  185.     void characterSetG::
  186. subtract (const setG* S)
  187.    {for(unsigned char C = 0; C <= 255; ++C)
  188.        {if(getbit(C))
  189.             if(S->contains(characterA::make(C).guts()))
  190.                 clearbit(C);        
  191.        }
  192.    }
  193.  
  194.     void characterSetG::
  195. intersect (const setG* S)
  196.    {for(unsigned char C = 0; C <= 255; ++C)
  197.        {if(getbit(C))
  198.             if(!S->contains(characterA::make(C).guts()))
  199.                 clearbit(C);        
  200.        }
  201.    }
  202.  
  203.  
  204. // finiteSet Operations //////////
  205.  
  206.     void characterSetG::
  207. invert ()
  208.    {flipbits();}
  209.  
  210.  
  211. //***************************************************************************
  212.